/**
 * Created by caiqyxyx on 2017/2/12.
 */
define(['durandal/app', 'durandal/composition', 'i18nCommon', 'http', 'jquery', 'knockout', 'BMap', 'box', 'panel', 'navhelper', 'echart', 'tree', 'view/law/statData',
        'css!style/law/law',
        'css!style/common/common-style',
        'css!library/3rdparty/font-awesome/css/font-awesome.min'],
    function (app, composition, i18n, http, $, ko, baiduMap, box, panal, nav, ec, tree, edata) {
        var law = new function () {
            var that = this;
            var mainMap;
            
            var getHumanInfoById = http.getInstance("law/human/getHumanInfoById");

            that.lawHomePage = ko.observable();
            that.lawEquipmentLocation = ko.observable();
            that.lawHumanTraj = ko.observable();
            that.lawCamera = ko.observable();
            that.logoutSystem = ko.observable();
            that.humanManager = ko.observable();
            that.humanSet = ko.observable();
            that.examOnline = ko.observable();

            composition.addBindingHandler('application-law', {
                init: function (dom) {
                    that._dom = $(dom);
                    that.lawBody = $('#application-law-body');
                    that.width = that.lawBody.width();
                    that.height = that.lawBody.height();
                    that.bindUI();
                    that.renderUI();
                    nav.startUI();
                    that.initData();
                }
            });

            that.bindUI = function () {  

            	getHumanInfoById.ajax({humanId: cywork.global.humanId}).then(function (data) {
                    if (data.human !== null) {
                    	var html = "";
                        if(data.human.dutyId === 1){
                        	html += "<a href='#' id='report'><i class='icon-bar-chart'></i>问题上报</a>";
                        }else{
                        	html += "<a href='#' id='eventMgr'><i class='icon-bar-chart'></i>运维管理</a>";
                        }
                        $("#reportLi").append(html);
                    }
                });           	 
                 
                $('.venus-menu').delegate('li', 'mouseover', function () {
                    var target = $(this);
                    if (!target.hasClass("active")) {
                        $('.venus-menu li').removeClass("active")
                        target.addClass("active");
                    }
                });               

                that.humanManager = function () {
                    that.humanLevelTree();
                    mainMap.clearOverlays();
                    that.locatePoint();
                }

                that.humanSet = function (){
                	//view/lawdynamic/lawhumantree   view/builder/human/human
                	var p = panal.getInstance('view/builder/human/human', {
                        width: 609,
                        height: 500,
                        top: 70,
                        title: "人员设置",
                        modal: false,
                        resizable: false
                    }, {map: mainMap});
                };
                
                //主页按钮点击
                that.lawHomePage = function () {

                };

                //定位按钮点击
                that.lawEquipmentLocation = function () {

                };
                //轨迹按钮点击
                that.lawHumanTraj = function (i) {
                    var p = panal.getInstance('view/lawdynamic/lawinfo', {
                        width: 300,
                        height: 150,
                        bottom: '31px',
                        right: '2px',
                        title: "执法员信息",
                        modal: false,
                        resizable: false
                    }, {map: mainMap, humanId: i});
                };
                //视频按钮点击
                that.lawCamera = function () {

                };

                //登出系统
                that.logoutSystem = function () {
                    var logoutService = http.getInstance("logout");
                    box.alert("确定要退出系统吗?", "提醒", {
                        clickSure: function () {
                            logoutService.ajax().then(function () {
                                window.location = cywork.global.rootPath + 'main' + window.location.search;
                            });
                        }
                    });
                };

                //在线考核按钮
                that.examOnline = function () {
                    var stat = $("#law-stat-container");
                    if (stat.css("display") === undefined || stat.css("display") === "none") {
                        that.statUI(true);
                    } else {
                        that.statUI(false);
                    }
                }

                that._dom.delegate(".law-stat-container-btn",'click',function () {
                    var stat = $("#law-stat-container");
                    if (stat.css("display") === undefined || stat.css("display") === "none") {
                        that.statUI(true);
                    } else {
                        that.statUI(false);
                    }
                });
                
                that._dom.delegate("#report",'click', function () {
                	var p = panal.getInstance('view/business/reportevent', {
                        width: 370,
                        height: 255,
                        top: '120px',
                        title: "运维管理",
                        modal: false,
                        resizable: false
                    }, {map: mainMap});
                });
                that._dom.delegate("#eventMgr",'click', function () {
                	var p = panal.getInstance('view/business/eventMgr', {
                        width: 600,
                        height: 460,
                        top: '100px',
                        title: "上报管理",
                        modal: false,
                        resizable: false
                    }, {map: mainMap});
                });
            };

            that.renderUI = function () {
                that.initMap();
                //that.humanLevelTree();
                //that.statUI();

            };

            that.echartInit = function (id, opts) {
                var myChart = ec.init(document.getElementById(id));
                //option = {};
                myChart.setOption(opts);
            };



            that.statUI = function (showFlag) {
                if (showFlag) {
                    if ($("#law-stat-container").css("display") === undefined) {
                        var html = $('<div id="law-stat-container" class="law-stat-container" style="position: absolute;top: 70px;bottom: 10px;left: 10px;right: 10px;opacity: 0.9;background-color: white;display: none;">' +
                            '<div class="law-stat-container-btn" title="关闭考核" style="position: absolute; top: 2px;right: 2px;width: 22px;height: 22px;cursor: pointer;z-index: 1;border-radius: 11px;background-color: white;padding: 1px;">' +
                                "<svg width=\'20\' height=\'22\' version=\'1.1\' xmlns=\'http://www.w3.org/2000/svg\' class=\'app-box-head-button-btn app-box-head-button-btn-close\'>" +
                                    "<line x1=\'5\' y1=\'5\' x2=\'15\' y2=\'15\'  style=\'stroke:#b40e0f;stroke-width:2\'/>" +
                                    "<line x1=\'15\' y1=\'5\' x2=\'5\' y2=\'15\'  style=\'stroke:#b40e0f;stroke-width:2\'/>" +
                                "</svg>",
                            '</div>' +
                            '</div>');
                        var stat1 = $("<div id='law-stat-1' style='float: left;box-shadow: 1px 1px 8px black;'></div>");
                        var stat2 = $("<div id='law-stat-2' style='float: left;margin-left:10px;box-shadow: 1px 1px 8px black;'></div>");
                        var stat3 = $("<div id='law-stat-3' style='margin-top: 10px;float: left;box-shadow: 1px 1px 8px black;'></div>");

                        $(html).append(stat1);
                        $(html).append(stat2);
                        $(html).append(stat3);

                        that._dom.append($(html));
                        $("#law-stat-container").show(500, function () {
                            var width = $(html).width();
                            var height = $(html).height();
                            var statwidth = width / 2;
                            var statheight = height / 2 - 10;

                            stat1.width(statwidth - 5);
                            stat1.height(statheight);

                            stat2.width(statwidth - 5);
                            stat2.height(statheight);

                            stat3.width(width)
                            stat3.height(statheight - 10)

                            that.echartInit('law-stat-1', edata.getshichang);

                            that.echartInit('law-stat-2', edata.getjuli);

                            that.echartInit('law-stat-3', edata.getxungeng);

                        });
                    } else {
                        $("#law-stat-container").show(500);
                    }
                } else {
                    $("#law-stat-container").hide(500);
                }
            };

            that.humanLevelTree = function () {
                var p = panal.getInstance('view/lawdynamic/lawhumantree', {
                    width: 300,
                    height: 550,
                    top: '65px',
                    left: '2px',
                    title: "执法员管理",
                    modal: false,
                    resizable: false
                }, {map: mainMap});

            };

            that.initData = function () {

                $('#humanName').html(cywork.global.humanName);
                $('#unitName').html(cywork.global.unitName);              
            }

            /**
             * 初始化地图实例
             */
            that.initMap = function () {
                var map = new BMap.Map("application-map", {enableMapClick: false});
                var point = new BMap.Point(116.331398, 39.897445);
                map.centerAndZoom(point, 12);
                map.enableScrollWheelZoom();
                mainMap = map;

                var top_left_control = new BMap.ScaleControl({anchor: BMAP_ANCHOR_BOTTOM_LEFT});// 左下角，添加比例尺
                var top_left_navigation = new BMap.NavigationControl({anchor: BMAP_ANCHOR_BOTTOM_LEFT});

                map.addControl(top_left_control);
                map.addControl(top_left_navigation);

                // 创建地址解析器实例
                var myGeo = new BMap.Geocoder();
                // 将地址解析结果显示在地图上,并调整地图视野
                myGeo.getPoint("宁波市", function (_point) {
                    if (_point) {
                        map.centerAndZoom(_point, 14);
                    } else {
                    }
                }, "宁波市");

                that.locatePoint();
            };

            that.locatePoint = function () {
                var points = [
                    new BMap.Point(121.555177, 29.884123), new BMap.Point(121.564519, 29.894393),
                    new BMap.Point(121.584785, 29.882118), new BMap.Point(121.57573, 29.869842),
                    new BMap.Point(121.587372, 29.873976), new BMap.Point(121.558195, 29.872223),
                    new BMap.Point(121.572568, 29.879363), new BMap.Point(121.604045, 29.863328),
                    new BMap.Point(121.595421, 29.874728), new BMap.Point(121.566244, 29.882494),
                    new BMap.Point(121.585504, 29.879488), new BMap.Point(121.540804, 29.87811),
                    new BMap.Point(121.605194, 29.881116), new BMap.Point(121.597146, 29.86007),
                    new BMap.Point(121.54181, 29.894644), new BMap.Point(121.620861, 29.872974),
                    new BMap.Point(121.58342, 29.831861)
                ];
                for (var i = 0; i < points.length; ++i) {
                    var myIcon = new BMap.Icon(cywork.global.rootPath + "style/common/images/lawhuman.png", new BMap.Size(20, 40));
                    var marker = new BMap.Marker(points[i], {icon: myIcon});        // 创建标注
                    mainMap.addOverlay(marker);                     // 将标注添加到地图中

                    marker.addEventListener("click", showLawInfo);
                }
            };

            function showLawInfo() {
                that.lawHumanTraj(2);
            }
        };

        return law;

    });